﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/2 23:54:23
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 应用程序管理类。
    /// </summary>
    public class Applications
    {
        private Applications() { }

        /// <summary>
        /// 创建应用程序。
        /// </summary>
        /// <typeparam name="TApplication">应用程序类。</typeparam>
        /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
        /// <param name="application">应用程序</param>
        /// <returns>返回执行结果。</returns>
        public static bool SaveApplication<TApplication, TConfiguration>(TApplication application)
            where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
            where TConfiguration : ConfigurationBase<TConfiguration>, new()
        {
            if (CommonDataProvider.Current.SaveApplication<TApplication, TConfiguration>(application)) {
                RemoveCache();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除应用程序。
        /// </summary>
        /// <param name="applicationID">应用程序唯一ID。</param>
        /// <returns>返回执行结果。</returns>
        public static bool DeleteApplication(string applicationID)
        {
            if (CommonDataProvider.Current.DeleteApplication(applicationID)) {
                RemoveCache();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 加载应用程序列表，键为应用程序ID。
        /// </summary>
        /// <returns>返回应用程序列表。</returns>
        public static Dictionary<string, ApplicationBase> LoadApplications()
        {
            return Caches.Get<Dictionary<string, ApplicationBase>>(allCacheKey, Globals.MinuteSeconds, Globals.MinuteSeconds, () =>
            {
                return CommonDataProvider.Current.LoadApplications();
            });
        }

        private static void RemoveCache() {
            Caches.Remove(allCacheKey);
            Caches.Remove(areaCacheKey);
        }

        private static readonly string allCacheKey = "Applications";

        /// <summary>
        /// 获取应用程序类型。
        /// </summary>
        /// <typeparam name="TApplication">应用程序类。</typeparam>
        /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
        /// <param name="applicationID">应用程序唯一ID。</param>
        /// <returns>返回应用程序类型。</returns>
        public static TApplication GetApplication<TApplication, TConfiguration>(string applicationID = null)
            where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
            where TConfiguration : ConfigurationBase<TConfiguration>, new()
        {
            if (applicationID == null)
                applicationID = new TApplication().ApplicationID;
            return CommonDataProvider.Current.GetApplication<TApplication, TConfiguration>(applicationID);
        }

        private static readonly string areaCacheKey = "Mozlite.Common.AllAreas";

        /// <summary>
        /// 加载所有区域。
        /// </summary>
        /// <returns></returns>
        public static List<string> LoadAreas()
        {
            return Caches.Get<List<string>>(areaCacheKey, Globals.MinuteSeconds, Globals.MinuteSeconds, () =>
            {
                return CommonDataProvider.Current.LoadAreas();
            });
        }

        /// <summary>
        /// 通过区域名称获取应用程序实例。
        /// </summary>
        /// <param name="areaName">区域名称。</param>
        /// <returns>返回应用程序实例对象。</returns>
        public static ApplicationBase GetApplication(string areaName = Routes.NullArea) {
            var applications = from application in LoadApplications().Values
                               where application.AreaName.IsMatched(areaName)
                               select application;
            return applications.FirstOrDefault<ApplicationBase>();
        }

        private static readonly string configurationCacheKey = "Mozlite.Common.Configuration::{0}::{1}";

        /// <summary>
        /// 保存应用程序数据。
        /// </summary>
        /// <typeparam name="TConfiguration">当前应用程序类型。</typeparam>
        /// <param name="configuration">应用程序实例对象。</param>
        /// <returns>返回是否执行成功。</returns>
        public static bool SaveConfiguration<TConfiguration>(TConfiguration configuration)
            where TConfiguration : ConfigurationBase<TConfiguration>, new()
        {
            if (CommonDataProvider.Current.SaveConfiguration<TConfiguration>(configuration))
            {
                Caches.Remove(string.Format(configurationCacheKey, configuration.SettingsID, configuration.ApplicationID));
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取应用程序数据配置实例。
        /// </summary>
        /// <param name="applicationID">应用程序ID。</param>
        /// <param name="settingsID">网站设置ID。</param>
        /// <returns>返回应用程序配置实例。</returns>
        public static ConfigurationBase GetConfiguration(string applicationID, int settingsID) {
            return Caches.Get<ConfigurationBase>(string.Format(configurationCacheKey, settingsID, applicationID), Globals.MinuteSeconds, Globals.MinuteSeconds, () => {
                return CommonDataProvider.Current.GetConfiguration(applicationID, settingsID);
            });
        }
    }
}